import http from '../http'
import { prefix } from '.'
import type { CanOperationData, MenuItem, PermissionResponse } from '../types'

/**
 * @description 查询菜单列表
 */
export const _queryMenus = params =>
  http.get<DataType<MenuItem[]>>(`${prefix}/menu/list`, {
    params
  })

/**
 * @description 新增菜单
 */
export const _addMenu = data => http.post(`${prefix}/menu/create`, { data })

/**
 * @description 查询菜单详情
 * @param id 菜单id
 * @returns 菜单详情
 */
export const _queryMenuDetail = (id: number) =>
  http.get<DataType<MenuItem>>(`${prefix}/menu/detail`, {
    params: { id }
  })

/**
 * @description 更新菜单
 * @param data 菜单信息
 */
export const _updateMenu = data => http.patch(`${prefix}/menu/update`, data)

/**
 * @description 删除菜单
 * @param id 需要删除的菜单的ID
 */
export const _deleteMenu = (id: number) =>
  http.delete(`${prefix}/menu/delete`, { params: { id } })

/**
 * @description 查询指定菜单下可进行的操作列表
 * @param params menuId 菜单id
 * @returns number[] 操作id列表
 */
export const _queryCanOperationByMenuId = (params: { menuId: number }) =>
  http.get<DataType<CanOperationData>>(
    `${prefix}/menu/findCanOperationByMenuId`,
    { params }
  )

/**
 * @description 绑定菜单可进行的操作列表
 * @param menuId menuId 菜单id
 * @param operationIds 需要绑定的操作id列表
 */
export const _bindMenuCanOperation = (data: {
  menuId: number
  operationCodes: string[]
}) => http.post(`${prefix}/menu/bindMenuOpeation`, data)

/**
 * 查询指定菜单下可进行的操作列表
 * @param params menuId 菜单id
 * @returns number[] 操作id列表
 */
export const _queryMenuBindOperations = (params: {
  menuId: number
  roleId: number
}) =>
  http.get<DataType<CanOperationData>>(
    `${prefix}/menu/findMenuBindOperations`,
    { params }
  )

/**
 * 查询指定菜单下的接口权限列表
 * @param menuId
 * @param keyword 接口名称关键字
 * @returns
 */
export const _queryMenuPermissions = (params: {
  menuId: number
  keyword: string
}) =>
  http.get<DataType<PermissionResponse[]>>(`${prefix}/menu/permission`, {
    params
  })

/**
 * 新增接口权限
 * @param data
 * @returns
 */
export const _addPermission = data =>
  http.post(`${prefix}/menu/permission/create`, data)

/**
 * 更新接口权限
 * @param data
 * @returns
 */
export const _updatePermission = data =>
  http.patch(`${prefix}/menu/permission/update`, data)

/**
 * 删除接口权限
 * @param {number} id
 * @returns
 */
export const _deletePermission = (id: number) =>
  http.delete(`${prefix}/menu/permission/delete`, { params: { id } })

/**
 * 查询角色已分配的菜单绑定的接口权限
 * @param {number} params.roleId roleId 角色id
 * @param {number} params.menuId menuId 菜单id
 * @returns
 */
export const _queryMenuBindPermissions = (params: {
  roleId: number
  menuId: number
}) => http.get(`${prefix}/menu/findMenuBindPermissions`, { params })
